package stdio.costa.imoca.spi;

import java.awt.Color;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import stdio.costa.imoca.model.ImageInfo;
import stdio.costa.imoca.model.TextInfo;
import stdio.costa.imoca.utils.ImageUtils;

@Component
public class PostcardFrontSpi implements InitializingBean {

	@Value("#{properties['receiver_postcode_1_x']}")
	private int postcodeX1;

	@Value("#{properties['receiver_postcode_2_x']}")
	private int postcodeX2;

	@Value("#{properties['receiver_postcode_3_x']}")
	private int postcodeX3;

	@Value("#{properties['receiver_postcode_4_x']}")
	private int postcodeX4;

	@Value("#{properties['receiver_postcode_5_x']}")
	private int postcodeX5;

	@Value("#{properties['receiver_postcode_6_x']}")
	private int postcodeX6;

	@Value("#{properties['receiver_postcode_y']}")
	private int postcodeY;

	@Value("#{properties['receiver_address_x']}")
	private int receiverAddressX;

	@Value("#{properties['receiver_address_y']}")
	private int receiverAddressY;

	@Value("#{properties['sender_address_x']}")
	private int senderAddressX;

	@Value("#{properties['sender_address_y']}")
	private int senderAddressY;

	@Value("#{properties['sender_postcode_x']}")
	private int senderPostcodeX;

	@Value("#{properties['sender_postcode_y']}")
	private int senderPostcodeY;

	@Value("#{properties['greeting_image_x']}")
	private int greetingImageX;

	@Value("#{properties['greeting_image_y']}")
	private int greetingImageY;

	@Value("#{properties['greeting_image_width']}")
	private int greetingImageWidth;

	@Value("#{properties['greeting_image_height']}")
	private int greetingImageHeight;

	@Value("#{properties['alpha']}")
	private float alpha;

	@Value("#{properties['receiver_postcode_font_name']}")
	private String receiverPostcodeFontName;

	@Value("#{properties['receiver_postcode_font_style']}")
	private int receiverPostcodeFontStyle;

	@Value("#{properties['receiver_postcode_font_size']}")
	private int receiverPostcodeFontSize;

	@Value("#{properties['receiver_postcode_font_color_r']}")
	private int receiverPostcodeFontColorR;

	@Value("#{properties['receiver_postcode_font_color_g']}")
	private int receiverPostcodeFontColorG;

	@Value("#{properties['receiver_postcode_font_color_b']}")
	private int receiverPostcodeFontColorB;

	@Value("#{properties['address_font_name']}")
	private String addressFontName;

	@Value("#{properties['address_font_style']}")
	private int addressFontStyle;

	@Value("#{properties['address_font_size']}")
	private int addressFontSize;

	@Value("#{properties['address_font_color_r']}")
	private int addressFontColorR;

	@Value("#{properties['address_font_color_g']}")
	private int addressFontColorG;

	@Value("#{properties['address_font_color_b']}")
	private int addressFontColorB;

	private Color receiverPostcodeFontColor;

	private Color addressFontColor;

	@Override
	public void afterPropertiesSet() throws Exception {

		// please ignore this
		this.receiverPostcodeFontColor = new Color(
				this.receiverPostcodeFontColorR,
				this.receiverPostcodeFontColorG,
				this.receiverPostcodeFontColorB);
		this.addressFontColor = new Color(this.addressFontColorR,
				this.addressFontColorG, this.addressFontColorB);
	}

	public Image createPostcardFront(InputStream greetingPicture,
			String postcode1, String postcode2, String postcode3,
			String postcode4, String postcode5, String postcode6,
			String receiverAddress, String senderAddress, String senderPostcode) {
		try {
			List<TextInfo> textInfoList = this.createPostcodeTextInfoList(
					postcode1, postcode2, postcode3, postcode4, postcode5,
					postcode6);

			textInfoList.add(this
					.createReceiverAddressTextInfo(receiverAddress));

			textInfoList.add(this.createSenderAddressTextInfo(senderAddress));

			textInfoList.add(this.createSenderPostcodeTextInfo(senderPostcode));

			ImageInfo imageInfo = new ImageInfo(greetingPicture,
					this.greetingImageX, this.greetingImageY,
					this.greetingImageWidth, this.greetingImageHeight, alpha);

			byte[] bs = ImageUtils.injectImageAndTextList(
					PostcardFrontSpi.class.getResourceAsStream("154_106.jpg"),
					imageInfo, textInfoList);

			Image image = ImageIO.read(new ByteArrayInputStream(bs));

			return image;

		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	private List<TextInfo> createPostcodeTextInfoList(String postcode1,
			String postcode2, String postcode3, String postcode4,
			String postcode5, String postcode6) {

		List<TextInfo> textInfoList = new ArrayList<TextInfo>();

		textInfoList.add(new TextInfo(postcode1, this.receiverPostcodeFontName,
				this.receiverPostcodeFontStyle, this.receiverPostcodeFontSize,
				this.receiverPostcodeFontColor, this.postcodeX1,
				this.postcodeY, this.alpha));

		textInfoList.add(new TextInfo(postcode2, this.receiverPostcodeFontName,
				this.receiverPostcodeFontStyle, this.receiverPostcodeFontSize,
				this.receiverPostcodeFontColor, this.postcodeX2,
				this.postcodeY, this.alpha));

		textInfoList.add(new TextInfo(postcode3, this.receiverPostcodeFontName,
				this.receiverPostcodeFontStyle, this.receiverPostcodeFontSize,
				this.receiverPostcodeFontColor, this.postcodeX3,
				this.postcodeY, this.alpha));

		textInfoList.add(new TextInfo(postcode4, this.receiverPostcodeFontName,
				this.receiverPostcodeFontStyle, this.receiverPostcodeFontSize,
				this.receiverPostcodeFontColor, this.postcodeX4,
				this.postcodeY, this.alpha));

		textInfoList.add(new TextInfo(postcode5, this.receiverPostcodeFontName,
				this.receiverPostcodeFontStyle, this.receiverPostcodeFontSize,
				this.receiverPostcodeFontColor, this.postcodeX5,
				this.postcodeY, this.alpha));

		textInfoList.add(new TextInfo(postcode6, this.receiverPostcodeFontName,
				this.receiverPostcodeFontStyle, this.receiverPostcodeFontSize,
				this.receiverPostcodeFontColor, this.postcodeX6,
				this.postcodeY, this.alpha));
		return textInfoList;
	}

	private TextInfo createReceiverAddressTextInfo(String receiverAddress) {
		return new TextInfo(receiverAddress, this.addressFontName,
				this.addressFontStyle, this.addressFontSize,
				this.addressFontColor, this.receiverAddressX,
				this.receiverAddressY, alpha);
	}

	private TextInfo createSenderAddressTextInfo(String senderAddress) {
		return new TextInfo(senderAddress, this.addressFontName,
				this.addressFontStyle, this.addressFontSize,
				this.addressFontColor, this.senderAddressX,
				this.senderAddressY, alpha);
	}

	private TextInfo createSenderPostcodeTextInfo(String senderPostcode) {
		return new TextInfo(senderPostcode, this.addressFontName,
				this.addressFontStyle, this.addressFontSize,
				this.addressFontColor, this.senderPostcodeX,
				this.senderPostcodeY, alpha);
	}
}
